﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SampleShop
{
    public abstract class AbstractUser : IUser
    {
        /// <summary>
        /// 用户Id
        /// </summary>
        public long UserId { get; set; }

        /// <summary>
        /// 用户名称
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 收藏的商户
        /// </summary>
        private IList<AbstractUser> merchants = new List<AbstractUser>();
        /// <summary>
        /// 收藏的商品
        /// </summary>
        private IList<IProduct> products = new List<IProduct>();

        /// <summary>
        /// 是否通知用户商品价格下降价格
        /// </summary>
        private bool isNotify { get; set; } 

        
        private void NotifyPriceLower(IProduct product)
        {
            Console.WriteLine($"降价通知：{UserName}您好！！商品{product.Name}价格下降了!!!目前价格为：{product.LowerPrice}元,原来价格{product.Price}元，一共降价：{product.Price-product.LowerPrice}元。");
        }

        /// <summary>
        /// 关注商户店铺
        /// </summary>
        /// <param name="merchant"></param>
        public void FocusMerchant(AbstractUser merchant)
        {
            merchants.Add(merchant);
            Console.WriteLine($"{UserName}关注了商户{merchant.UserName}的商铺");
        }

        /// <summary>
        /// 关注了商品
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="productId"></param>
        public void FocusProduct(IProduct product)
        {
            if (isNotify)
                product.PriceChangeLow += NotifyPriceLower;
            products.Add(product);
            Console.WriteLine($"{UserName}关注了商品编号为{product.Id}的商铺");
        }


        /// <summary>
        /// 设置当收藏的商品价格下降时是否通知用户
        /// </summary>
        /// <param name="isNotify"></param>
        public void IsNotify(bool isNotify)
        {
            this.isNotify = isNotify;
            var message = isNotify ? $"开启" : "关闭";
            Console.WriteLine($"{UserName}{message}了消息通知！");
            //遍历目前所有的收藏的商品，订阅商品价格变动事件
            if (!isNotify) return;
            foreach (var item in products)
            {
                item.PriceChangeLow += NotifyPriceLower;
            }
            

        }

        public virtual void ShowMyProducts()
        {
            Console.Write("我收藏的商品有：");
            ShowProducts();
            
        }
        protected void ShowProducts()
        {
            for (int i = 0; i < products.Count; i++)
            {
                if (i == products.Count - 1)
                {
                    Console.WriteLine(products[i].Name);
                }
                else
                {
                    Console.Write(products[i].Name + "、");
                }
            }
            Console.WriteLine();
        }
    }
}
